import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.text.DecimalFormat;

/**
 * StoreUI class that displays information for the store it is constructed for. Allows the user the purchase items.
 * @author Taylor Holden
 *
 */
public class StoreUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private boolean hasFinished;
	private JTextField[] textFields;
	private int[] quants;

	/**
	 * Create the frame.
	 * 
	 * @param s Store instance that the frame is designed around.
	 */
	public StoreUI(Store s) {
		hasFinished = false;
		quants = new int[7];
		DecimalFormat df = new DecimalFormat("$#,###,##0.00");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Store");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("Arial", Font.PLAIN, 24));
		lblNewLabel.setBounds(265, 11, 231, 55);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Oxen");
		lblNewLabel_1.setFont(new Font("Arial", Font.PLAIN, 11));
		lblNewLabel_1.setBounds(27, 127, 114, 14);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("Food");
		lblNewLabel_2.setFont(new Font("Arial", Font.PLAIN, 11));
		lblNewLabel_2.setBounds(27, 167, 91, 14);
		contentPane.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("Clothing");
		lblNewLabel_3.setFont(new Font("Arial", Font.PLAIN, 11));
		lblNewLabel_3.setBounds(27, 211, 91, 14);
		contentPane.add(lblNewLabel_3);
		
		JLabel lblNewLabel_4 = new JLabel("Ammunition");
		lblNewLabel_4.setFont(new Font("Arial", Font.PLAIN, 11));
		lblNewLabel_4.setBounds(27, 252, 114, 14);
		contentPane.add(lblNewLabel_4);
		
		JLabel lblNewLabel_5 = new JLabel("Wagon Tongues");
		lblNewLabel_5.setBounds(421, 211, 107, 14);
		contentPane.add(lblNewLabel_5);
		
		JLabel lblNewLabel_6 = new JLabel("Wagon Wheels");
		lblNewLabel_6.setBounds(431, 127, 97, 14);
		contentPane.add(lblNewLabel_6);
		
		JLabel lblNewLabel_7 = new JLabel("Wagon Axles");
		lblNewLabel_7.setBounds(431, 167, 97, 14);
		contentPane.add(lblNewLabel_7);
		
		JLabel lblNewLabel_8 = new JLabel("(x" + (s.getQuantities())[0] + ")");
		lblNewLabel_8.setBounds(149, 127, 46, 14);
		contentPane.add(lblNewLabel_8);
		
		JLabel lblNewLabel_9 = new JLabel("(x" + (s.getQuantities())[1] + ")");
		lblNewLabel_9.setBounds(149, 167, 46, 14);
		contentPane.add(lblNewLabel_9);
		
		JLabel lblNewLabel_10 = new JLabel("(x" + (s.getQuantities())[2] + ")");
		lblNewLabel_10.setBounds(149, 211, 46, 14);
		contentPane.add(lblNewLabel_10);
		
		JLabel lblNewLabel_11 = new JLabel("(x" + (s.getQuantities())[3] + ")");
		lblNewLabel_11.setBounds(149, 252, 46, 14);
		contentPane.add(lblNewLabel_11);
		
		JLabel lblNewLabel_12 = new JLabel("(x" + (s.getQuantities())[4] + ")");
		lblNewLabel_12.setBounds(538, 127, 46, 14);
		contentPane.add(lblNewLabel_12);
		
		JLabel lblNewLabel_13 = new JLabel("(x" + (s.getQuantities())[5] + ")");
		lblNewLabel_13.setBounds(538, 167, 46, 14);
		contentPane.add(lblNewLabel_13);
		
		JLabel lblNewLabel_14 = new JLabel("(x" + (s.getQuantities())[6] + ")");
		lblNewLabel_14.setBounds(538, 211, 46, 14);
		contentPane.add(lblNewLabel_14);
		
		textFields = new JTextField[7];
		
		textField = new JTextField();
		textField.setBounds(199, 124, 86, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		textFields[0] = textField;
		
		textField_1 = new JTextField();
		textField_1.setBounds(199, 164, 86, 20);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		textFields[1] = textField_1;
		
		textField_2 = new JTextField();
		textField_2.setBounds(199, 208, 86, 20);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		textFields[2] = textField_2;
		
		textField_3 = new JTextField();
		textField_3.setBounds(199, 249, 86, 20);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		textFields[3] = textField_3;
		
		textField_4 = new JTextField();
		textField_4.setBounds(594, 124, 86, 20);
		contentPane.add(textField_4);
		textField_4.setColumns(10);
		textFields[4] = textField_4;
		
		textField_5 = new JTextField();
		textField_5.setBounds(594, 164, 86, 20);
		contentPane.add(textField_5);
		textField_5.setColumns(10);
		textFields[5] = textField_5;
		
		textField_6 = new JTextField();
		textField_6.setBounds(594, 208, 86, 20);
		contentPane.add(textField_6);
		textField_6.setColumns(10);
		textFields[6] = textField_6;
		
		JButton btnNewButton = new JButton("Purchase");
		btnNewButton.setBounds(304, 413, 178, 55);
		contentPane.add(btnNewButton);
		btnNewButton.addActionListener(new PurchaseButtonListener());
		
		JLabel lblItem = new JLabel("Item");
		lblItem.setFont(new Font("Arial", Font.BOLD, 11));
		lblItem.setBounds(27, 88, 46, 14);
		contentPane.add(lblItem);
		
		JLabel lblQuantity = new JLabel("Quantity");
		lblQuantity.setFont(new Font("Arial", Font.BOLD, 11));
		lblQuantity.setBounds(134, 88, 86, 14);
		contentPane.add(lblQuantity);
		
		JLabel lblOrder = new JLabel("Order");
		lblOrder.setFont(new Font("Arial", Font.BOLD, 11));
		lblOrder.setBounds(220, 88, 46, 14);
		contentPane.add(lblOrder);
		
		JLabel label = new JLabel("Item");
		label.setFont(new Font("Arial", Font.BOLD, 11));
		label.setBounds(421, 88, 46, 14);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("Quantity");
		label_1.setFont(new Font("Arial", Font.BOLD, 11));
		label_1.setBounds(525, 88, 86, 14);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("Order");
		label_2.setFont(new Font("Arial", Font.BOLD, 11));
		label_2.setBounds(617, 88, 46, 14);
		contentPane.add(label_2);
		
		JLabel lblPrice = new JLabel("Price");
		lblPrice.setFont(new Font("Arial", Font.BOLD, 11));
		lblPrice.setBounds(319, 88, 46, 14);
		contentPane.add(lblPrice);
		
		JLabel lblNewLabel_15 = new JLabel(df.format((s.getCosts())[0] / 100.0));
		lblNewLabel_15.setBounds(319, 127, 46, 14);
		contentPane.add(lblNewLabel_15);
		
		JLabel lblNewLabel_16 = new JLabel(df.format((s.getCosts())[1] / 100.0));
		lblNewLabel_16.setBounds(319, 167, 46, 14);
		contentPane.add(lblNewLabel_16);
		
		JLabel lblNewLabel_17 = new JLabel(df.format((s.getCosts())[2] / 100.0));
		lblNewLabel_17.setBounds(319, 211, 46, 14);
		contentPane.add(lblNewLabel_17);
		
		JLabel lblNewLabel_18 = new JLabel(df.format((s.getCosts())[3] / 100.0));
		lblNewLabel_18.setBounds(319, 252, 46, 14);
		contentPane.add(lblNewLabel_18);
		
		JLabel lblNewLabel_19 = new JLabel(df.format((s.getCosts())[4] / 100.0));
		lblNewLabel_19.setBounds(712, 127, 46, 14);
		contentPane.add(lblNewLabel_19);
		
		JLabel lblNewLabel_20 = new JLabel(df.format((s.getCosts())[5] / 100.0));
		lblNewLabel_20.setBounds(712, 167, 46, 14);
		contentPane.add(lblNewLabel_20);
		
		JLabel lblNewLabel_21 = new JLabel(df.format((s.getCosts())[6] / 100.0));
		lblNewLabel_21.setBounds(712, 211, 46, 14);
		contentPane.add(lblNewLabel_21);
		
		JLabel label_3 = new JLabel("Price");
		label_3.setFont(new Font("Arial", Font.BOLD, 11));
		label_3.setBounds(712, 88, 46, 14);
		contentPane.add(label_3);
	}
	
	/**
	 * Setter for the hasFinished boolean.
	 * @param b New value of hasFinished.
	 */
	public void setHasFinished(boolean b)
	{
		hasFinished = b;
	}
	
	/**
	 * Accessor for the hasFinished field.
	 * @return hasFinished boolean variable.
	 */
	public boolean hasFinished()
	{
		return hasFinished;
	}
	
	/**
	 * Accessor for the quants field.
	 * @return List of desired quantities of items.
	 */
	public int[] getQuants()
	{
		return quants;
	}
	
	class PurchaseButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			//System.err.println("Action fired!");
			if(!(textField.getText().equals("") && textField_1.getText().equals("") && textField_2.getText().equals("") && textField_3.getText().equals("") && textField_4.getText().equals("") && textField_5.getText().equals("") && textField_6.getText().equals("")))
			{
				//System.err.println("Boolean checked out");
				for(int i = 0; i < textFields.length; i++)
				{
					try
					{
						quants[i] = Integer.parseInt(textFields[i].getText());
					} catch(NumberFormatException e) { quants[i] = 0; }
				}
				
				hasFinished = true;
			}
		}
	}
}
